/* 
 * GrayRabbit Cms基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department：运营部
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.dao.site.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.ConstantOperCode;
import org.grayrabbit.cms.dao.site.SiteDao;
import org.grayrabbit.cms.entity.Site;
import org.grayrabbit.cms.vo.site.SiteBean;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import org.grayrabbit.dao.impl.GenericHibernateDao;
import org.grayrabbit.dao.util.Page;
import org.grayrabbit.dao.util.QueryCondition;

/**
 * <b>Site</b> dao implementation 





 * @version 1.0，创建时间：2011-05-16 15:11:17 
 */
@Repository
public class SiteDaoImpl extends GenericHibernateDao<Site, String>
		implements SiteDao {

	/**
	 * HQL主体语句
	 */
	public static final String MAIN_HQL = "From Site as obj";
	/**
	 * HQL中count统计hql片段
	 */
	public static final String COUNT_HQL = "Select Count(obj) ";

	/**
	 * 根据bean组装查询条件
	 * @see SiteDao#assembly(SiteBean)
	 */
	public QueryCondition assembly(SiteBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件
	 * @see SiteDao#assemblyCount(SiteBean)
	 */
	public QueryCondition assemblyCount(SiteBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * @see SiteDao#count(SiteBean)
	 */ 
	public int count(SiteBean bean) {
		return ((Long)iterator(assemblyCount(bean)).next()).intValue();
	}

	/**
	 * 根据bean组装查询条件(名称参数)
	 * @see SiteDao#assemblyNameQuery(SiteBean)
	 */
	public QueryCondition assermblyNameQuery(SiteBean bean) {
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		transNameQuery(bean, qc);
		return qc;
	}

	/**
	 * 根据bean组装统计查询条件(名称参数)
	 * @see SiteDao#assemblyNameQueryCount(SiteBean)
	 */
	public QueryCondition assemblyNameQueryCount(SiteBean bean) {
		QueryCondition qc = new QueryCondition(COUNT_HQL + MAIN_HQL);
		transNameQuery(bean,qc);
		return qc;
	}

	/**
	 * 填充分页组件
	 * @see SiteDao#findByPage(Page, SiteBean)
	 */
	public List<Site> findByPage(Page<Site> page,
			SiteBean bean) {
		QueryCondition qc = assembly(bean);
		qc.setMaxResults(page.getPageSize());
		qc.setOffset(page.getOffset());
		return findByQc(qc);
	}
	/**
	 * 查询所有记录
	 * @see SiteDao#findByPage(SiteBean)
	 */
	public List<Site> findByPage(
			SiteBean bean) {
		QueryCondition qc = assembly(bean);
		qc.append(" order by obj.siteStatus desc");
		return findByQc(qc);
	}
	// 业务查询主要逻辑代码
	private void transNameQuery(SiteBean bean,QueryCondition qc){
		if(bean!=null){
			if(StringUtils.isNotBlank(bean.getSiteName())){
				qc.add("And obj.siteName like ? ","%"+ bean.getSiteName()+"%");
			}
		}
	}
	/**
	 * 检查站点名称唯一性
	 */
	public Boolean checkNameUnique(String siteName,String stname){
		boolean unique=false;
		List<Site> siteList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(siteName)){
			qc.add(" And obj.siteName =?", siteName);
			siteList=findByQc(qc);
		}
		if(StringUtils.isNotBlank(stname) && !siteList.isEmpty()){
			for(int i=0;i<siteList.size();i++){
				if(siteList.get(i).getSiteName().equals(stname)){
					siteList.remove(i);
				}
			}
		}
		if((siteList!=null)&&(!siteList.isEmpty())){
			unique=true;
		}
		return unique;
	}
	/**
	 * 检查站点简称名称唯一性
	 */
	public Boolean checkSimplenameunique(String siteShortName,String oldname){
		boolean unique=false;
		List<Site> shortNameList=null;
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		if(StringUtils.isNotBlank(siteShortName)){
			qc.add(" And obj.siteShortName =?", siteShortName);
			shortNameList=findByQc(qc);
		}
		if(StringUtils.isNotBlank(oldname) && !shortNameList.isEmpty()){
			for(int i=0;i<shortNameList.size();i++){
				if(shortNameList.get(i).getSiteShortName().equals(oldname)){
					shortNameList.remove(i);
				}
			}
		}
		if((shortNameList!=null)&&(!shortNameList.isEmpty())){
			unique=true;
		}
		return unique;
	}

	public List<Site> findAllUsableSite(){
		QueryCondition qc = new QueryCondition(MAIN_HQL);
		qc.add(" And obj.siteStatus = ?", true);	
		qc.appenOrderBy(" obj.createTime desc ");
		return  findByQc(qc) ;
	}


	public List<Site> findByTypeAndId(String type, String ids) {
		if(type.startsWith(ConstantOperCode.SITE_FLAG)){
			QueryCondition qc = new QueryCondition(MAIN_HQL);
			qc.add("and obj.siteStatus=?", Boolean.TRUE);
			qc.append("and obj.id in(");
			qc.append(ids);
			qc.append(")");
			return this.findByQc(qc);
		}else if(type.startsWith(ConstantOperCode.CAT_FLAG)){
			QueryCondition qc = new QueryCondition("Select obj.site From Category as obj ");
			qc.add("and obj.site.siteStatus=?", Boolean.TRUE);
			qc.append("and obj.id in(");
			qc.append(ids);
			qc.append(")");
			return this.findByQc(qc);
		}else if(type.startsWith(ConstantOperCode.TOPIC_FLAG)){
			QueryCondition qc = new QueryCondition("Select obj.site From Topic as obj ");
			qc.add("and obj.site.siteStatus=?", Boolean.TRUE);
			qc.append("and obj.id in(");
			qc.append(ids);
			qc.append(")");
			return this.findByQc(qc);
		}
		return null;
	}

	public boolean findSiteByTheme(String themeName) {
		if(StringUtils.isBlank(themeName))  return false;
		QueryCondition qc = new QueryCondition(COUNT_HQL+MAIN_HQL);
		qc.add(" and obj.siteTheme=? ", themeName);
		int n = ((Long)iterator(qc).next()).intValue();
	    return n>0;
	}

	public String findSiteDomain(String siteArea) {
		Criteria criteria = getSession().createCriteria(Site.class);
		criteria.add(Restrictions.eq("area", siteArea));
		criteria.setProjection(Projections.property("siteDomain"));
		criteria.setMaxResults(1);
		return (String)criteria.uniqueResult();
	}
}